/**
 * Created by LeungZ on 2016/10/17.
 */
class Ctrl {
    constructor(imageSrv,userSrv,appSrv,$rootScope, $scope, ctgNotification) {
        "ngInject";
        this._imageSrv = imageSrv;
        this._appSrv = appSrv;
        this._userSrv= userSrv;
        this._$scope=$scope;
        this._notification = ctgNotification;
        this.imageType = "002";
        this.service = $scope.service;
        if ($scope.service.appImg && $scope.service.appImg.imageName) {
            this.getImgVerList($scope.service.appImg.imageName);
            this.getConfigs($scope.service.appImg.ver);
            this.getConfigVers($scope.service.appImg.imageConfig);
            //  console.log($scope.service.appImg);
        } 
         this.user={
           userCode:$rootScope.user.sysUserCode   
        };
        this.page = {
            currentPage: 1,
            totalItems: 0,
            pageSize:999
        };
    
        this. getIsAdmin();
        this. getImageList();
    } 
    //是否管理员登录  
    getIsAdmin(){
        this._imageSrv.isAdmin().then(data=>{
        if(data==true){                      
              this._$scope.searchBtn=true;
              this._userSrv.getUserList({currentPage: this.page.currentPage}).then(data=>{
              this.service.username=data.list;
              })
            }else{
              this._$scope.searchBtn=false; 
            } 
       }).catch((data)=>{
            this._ctgNotification.error('获取用户数据有误.');
       }) 
    }
    //获取用户的数据
     getBase(obj) {
        this._$scope.service.appImg = "";
        this.configList = null; //禁用请选择配置文件下拉框
        this.configVerList = null; //禁用配置文件的请选择版本
        this.appImgVerList=null;
        this.table = null;
        this._imageSrv.getHarborImages({ currentPage: this.page.currentPage,pageSize:this.page.pageSize,imageType:this.imageType, sysUserCode: this.user.userCode}).then(data => {
            this.service.table = data.list;
        }).catch(() => {
            this._notification.error('获取Docker镜像列表数据有误.');
        });
    }
    // 镜像类型
    getImageList(obj) {  
        this._$scope.service.appImg = "";  //清空ng-module的数据，解决下拉框空白
        this.configList = null; //禁用请选择配置文件下拉框
        this.appImgVerList=null;
        this._$scope.imageType = this.imageType;
        this.verLoading = true;
        if(this.imageType=='003'){
           this._$scope.searchBtn=false;
        }else{
           this.getIsAdmin(); 
        }
        this._appSrv.getBuildImages({currentPage: this.page.currentPage,pageSize:this.page.pageSize,imageType:this.imageType, sysUserCode: this.user.userCode}).then((data) => {
            this.service.table = data.list;
        }).catch(() => {
            this._notification.error("获取镜像列表有误。");
        }).finally(() => {
            this.verLoading = false;
        })
    }

    getImgVerList(imageName) {
        this.verLoading = true;
        this._imageSrv.getImageVers(imageName, this.imageType).then(data => {
            this.appImgVerList = data;
            if (this.service.appImg && this.service.appImg.ver) this.service.appImg.ver = _.findWhere(this.appImgVerList, { id: this.service.appImg.ver.id });
        }).catch(data => {
            this._notification.error("获取镜像版本数据有误, 请重新选择应用镜像");
        }).finally(() => {
            this.verLoading = false;
        })
    }

    getAppImgVer(imageName) {    
        // appImg.imageName = name;
        // appImg.ver = null;
        if(imageName==undefined){
            this.appImgVerList = null;      
        }else{ 
            this.getImgVerList(imageName);
        }      
    }
    // 应用镜像的请选择版本
    getConfigs(v) {
        this.configverLoading = true;
        this.configList = null;
        this.configVerList = null;
        if(v==undefined){
            // this.imageConfigVer = null; 
            this.configList = null; //禁用请选择配置文件下拉框
            this.configVerList = null; //禁用配置文件的请选择版本
        }else{
            this._imageSrv.getConfigName({ dockerImageTagId: v.id }).then((data) => {
                this.configList = data;
                if (this.service.appImg && this.service.appImg.imageConfig) this.service.appImg.imageConfig = _.findWhere(this.configList, { configName: this.service.appImg.imageConfig.configName });
                // if (data.length == 0) {
                //     this._notification.error("此镜像没有配置文件，请到镜像管理增加配置文件，或选择其他镜像。");
                // }
            }).catch(() => {
                this._notification.error("获取配置文件有误");
            }).finally(() => {
                this.configverLoading = false;
            })
        }
    }
    // 配置文件
    getConfigVers(v) {
        this.configverLoading = true;
        this.configVerList = null;
        if(v==undefined){
            this.configVerList = null; //禁用配置文件的请选择版本
            this.configList = null;
        }else{
            this._imageSrv.getConfigVer({ dockerImageTagId: v.dockerImageTagId, config_name: v.configName }).then((data) => {
                this.configVerList = data;
                if (this.service.appImg && this.service.appImg.imageConfigVer) this.service.appImg.imageConfigVer = _.findWhere(this.configVerList, { configVersion: this.service.appImg.imageConfigVer.configVersion });
            }).catch(() => {
                this._notification.error("获取配置文件版本数据有误");
            }).finally(() => {
                this.configverLoading = false;
            })
        }
       
    }
    getConfigsData(v) {
        this.configverLoading = true;
        if(v==undefined){
            
        }else{
            this._imageSrv.getConfigsData({ dockerImageTagId: v.dockerImageTagId, config_name: v.configName, config_version: v.configVersion }).then((data) => {
                var config = JSON.parse(data[0].config);
                angular.forEach(config, (en) => {
                    en.hide = true; //添加一个隐藏属性
                    delete en.remark;
                });
                this.service.envs = config;
            }).catch(() => {
                this._notification.error("获取配置文件环境变量数据有误。");
            }).finally(() => {
                this.configverLoading = false;
            })
        }
        
    }
}
export default Ctrl